﻿using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using SER = CPPEI.ETL.Domain.Services;
using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using VIW = CPPEI.ETL.Domain.Views;

namespace CPPEI.ETL.WebApi.DVPlugIn
{
    /// <summary>
    /// 获取数据审核插件列表
    /// </summary>
    [CPPEI.WebApi.AuthGetAction("DVPLUGIN", "GetPlugInList", "1.0")]
    public class GetPlugInListAction : BaseAction<GetPlugInListParams, GetPlugInListResult>
    {
        protected override ActionResult<GetPlugInListResult> OnExecute(ActionContext context,
            SysParameters sysParameters,
            GetPlugInListParams opParams)
        {
            SER.DVPlugInService service = new SER.DVPlugInService();

            PagedList<ENT.EDVPlugIn> serResult = service.GetPlugInList(opParams.AssemblyId, opParams.Page * 25, 25);

            if(serResult.IsSuccess)
            {
                GetPlugInListResult result = new GetPlugInListResult();

                result.Total = serResult.Total;
                result.Limit = serResult.Limit;
                result.Start = serResult.Start;
                result.Page = opParams.Page;

                foreach (var model in serResult.Data)
                {
                    GetPlugInListResult.Item item = new GetPlugInListResult.Item();
                    item.Id = model.Id;
                    item.Name = DefaultValue.GetValue(model.Name, string.Empty);
                    item.Description = DefaultValue.GetValue(model.Description, string.Empty);

                    result.Items.Add(item);
                }
                return new ActionResult<GetPlugInListResult>(result);
            }
            else
            {
                return new ActionResult<GetPlugInListResult>(serResult.IsSuccess, serResult.ErrorCode, serResult.Message, null);
            }
        }
    }

    public class GetPlugInListParams
    {
        public Guid AssemblyId { get; set; }
        public int Page { get; set; }
    }

    public class GetPlugInListResult : PagedResult
    {
        public List<Item> Items { get; set; }

        public GetPlugInListResult()
        {
            Items = new List<Item>();
        }

        public class Item
        {
            public Guid Id { get; set; }

            public string Name { get; set; }

            public string Description { get; set; }
        }
    }
}
